library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5 ✓ purrr 0.3.4
## ✓ tibble 3.1.3 ✓ dplyr 1.0.7
## ✓ tidyr 1.1.3 ✓ stringr 1.4.0
## ✓ readr 2.0.1 ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(ggthemes)
## Levantamos dataset
Individual_T120 <- read.table(paste0("../Fuentes/usu_individual_T120.txt"),
sep=";", dec=",", header = TRUE, fill = TRUE)
Podemos hacer una nueva versión del gráfico 2. Utilizando un procesamiento similar al que hicimos antes.
datagraf <- Individual_T120 %>%
select(REGION,P47T,T_VI, TOT_P12, P21, PONDII, CH04) %>%
filter(!is.na(P47T), P47T > 0 ) %>%
mutate(REGION = case_when(REGION == 1 ~ 'GBA',
REGION == 40 ~ 'NOA',
REGION == 41 ~ 'NEA',
REGION == 42 ~ 'Cuyo',
REGION == 43 ~ 'Pampeana',
REGION == 44 ~ 'Patagonia',
FALSE ~ 'otro'),
ingreso_laboral = as.numeric(TOT_P12 + P21),
ingreso_no_laboral = as.numeric(T_VI),
CH04 = case_when(CH04 == 1 ~ "Varon",
CH04 == 2 ~ "Mujer",
FALSE ~ "Otro") ) %>%
gather(., key = Tipo_ingreso, Ingreso, c((ncol(.)-1):ncol(.)))
datagraf
Con los Kernels, no necesitamos dividir a la población en deciles, porque podemos tener una mirada completa de la forma de la distribución.
Para este gráfico, quiero eliminar los ingresos = 0.
datagraf2 <- datagraf %>% filter(Ingreso !=0)
ggplot(datagraf2, aes(
x = Ingreso,
weights = PONDII,
group = Tipo_ingreso,
fill = Tipo_ingreso)) +
geom_density(alpha=0.7,adjust =2)+
labs(x="Distribución del ingreso", y="",
title=" Total según tipo de ingreso y género",
caption = "Fuente: Encuesta Permanente de Hogares - INDEC")+
scale_x_continuous(limits = c(0,50000))+
theme_tufte()+
scale_fill_gdocs()+
theme(legend.position = "bottom",
plot.title = element_text(size=12))+
facet_wrap(~ CH04, scales = "free") # se distingue por sexo
## Warning: Removed 2508 rows containing non-finite values (stat_density).
En este tipo de gráficos, importa mucho qué variable se utiliza para facetear y qué variable para agrupar, ya que la construcción de la distribución es diferente.
ggplot(datagraf2, aes(
x = Ingreso,
weights = PONDII,
group = CH04,
fill = CH04)) +
geom_density(alpha=0.7,adjust =2)+
labs(x="Distribución del ingreso", y="",
title=" Total según tipo de ingreso y género",
caption = "Fuente: Encuesta Permanente de Hogares")+
scale_x_continuous(limits = c(0,50000))+
theme_tufte()+
scale_fill_gdocs()+
theme(legend.position = "bottom",
plot.title = element_text(size=12))+
facet_wrap(~Tipo_ingreso, scales = "free") # se separa por ingreso
## Warning: Removed 2508 rows containing non-finite values (stat_density).
El eje y no tiene demasiada interpretabilidad en los Kernel, porque hace a la forma en que se construyen las distribuciones.
Para realizar estos gráficos, vamos a modificar un poco los datos:
levels(). El “\n”" es un caracter especial que permite que el string continúe en la siguiente línea.ggdata <- Individual_T120 %>%
filter(P21>0,
!is.na(NIVEL_ED),
NIVEL_ED!=7,
PP04A !=3) %>%
mutate(NIVEL_ED = factor(case_when(NIVEL_ED == 1 ~ 'Primaria \n Incompleta', # '\n' significa carriage return, o enter
NIVEL_ED == 2 ~ 'Primaria \n Completa',
NIVEL_ED == 3 ~ 'Secundaria \nIncompleta',
NIVEL_ED == 4 ~ 'Secundaria \nCompleta',
NIVEL_ED == 5 ~ 'Superior \nUniversitaria \nIncompleta',
NIVEL_ED == 6 ~ 'Superior \nUniversitaria \nCompleta',
FALSE ~ 'Otro'),
levels= c('Primaria \n Incompleta',
'Primaria \n Completa',
'Secundaria \nIncompleta',
'Secundaria \nCompleta',
'Superior \nUniversitaria \nIncompleta',
'Superior \nUniversitaria \nCompleta')),
Sexo = case_when(CH04 == 1 ~ 'Varón',
CH04 == 2 ~ 'Mujer'),
Establecimiento = case_when(PP04A == 1 ~ 'Estatal',
PP04A == 2 ~ 'Privado',
FALSE ~ 'Otro'))
ggdata